package com.github.ecbp.user.service.controller.front;

import com.github.ecbp.common.api.CommonResult;
import com.github.ecbp.common.data.builder.CommonPageBuilder;
import com.github.ecbp.common.data.controller.BaseController;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.user.service.dto.ButtonBo;
import com.github.ecbp.user.service.dto.MenuBo;
import com.github.ecbp.user.service.dto.MenuDto;
import com.github.ecbp.user.service.dto.MenuQueryDto;
import com.github.ecbp.user.service.response.MenuRes;
import com.github.ecbp.user.service.service.MenuService;
import com.github.ecbp.user.service.vo.ButtonNode;
import com.github.ecbp.user.service.vo.MenuNode;
import com.github.ecbp.user.service.vo.MenuPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.github.ecbp.common.constant.CacheKeyConstant.PROJECT_NAME;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author zj2626
 * @since 2020-03-18
 */
@RequestMapping("/api/" + PROJECT_NAME + "/menu")
@Api(tags = "后台菜单管理")
@RestController
public class MenuController extends BaseController {
    @Autowired
    protected MenuService service;

    @ApiOperation(value = "添加后台菜单", notes = "zhangj")
    @PostMapping(value = "/create")
    public CommonResult create(@Validated @RequestBody MenuDto param) {
        service.create(param);
        return CommonResult.succeed();
    }

    @ApiOperation(value = "修改后台菜单", notes = "zhangj")
    @PostMapping(value = "/update")
    public CommonResult update(@Validated(BaseIdDTO.NotNullId.class) @RequestBody MenuDto param) {
        service.updateSelectiveById(param);
        reloadPermission(param.getId());
        return CommonResult.succeed();
    }

    @ApiOperation(value = "根据ID获取菜单详情", notes = "zhangj", response = MenuRes.class)
    @PostMapping(value = "/info")
    public CommonResult getItem(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        return CommonResult.succeed(service.getById(param.getId()));
    }

    @ApiOperation(value = "根据ID删除后台菜单", notes = "zhangj")
    @PostMapping(value = "/delete")
    public CommonResult delete(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        service.removeMenuById(param);
        reloadPermission(param.getId());
        return CommonResult.succeed();
    }

    @ApiOperation(value = "分页列表-后台菜单", notes = "zhangj", response = MenuPageVo.class)
    @PostMapping(value = "/selectPage")
    public CommonResult selectPage(@Validated @RequestBody MenuQueryDto param) {
        return CommonResult.succeed(CommonPageBuilder.build(service.page(param)));
    }

    private void reloadPermission(Long id) {
        if (null != id) {
            service.reloadUserPermission(id);
        }
    }


    /***************************/
    /***************************/
    /***************************/

    @ApiOperation(value = "返回菜单下按钮", notes = "zhangj", response = ButtonNode.class)
    @PostMapping(value = "/buttonList")
    public CommonResult buttonList(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        List<ButtonNode> list = service.buttonList(new ButtonBo(param.getId()));
        return CommonResult.succeed(list);
    }

    /***************************/

    @ApiOperation(value = "树形结构返回所有菜单列表+按钮", notes = "zhangj", response = MenuNode.class)
    @PostMapping(value = "/treeListWithButton")
    public CommonResult treeListWithButton() {
        List<MenuNode> list = service.treeList(new MenuBo(true));
        return CommonResult.succeed(list);
    }

    /***************************/
    /***************************/
    /***************************/
}
